﻿CloudDisk.Admin.MemberPanel = (function(cfg){
    var el = $('#memberPanel');
	
	var memberItemTpl = $('#memberItemTpl');
	
	var members = [];
	
	var initialized = false;
	
    return {
	    init: function(){
		    var that = this;
		    el.find('.adm-member-toolbar button').click(function(){
			    CloudDisk.Admin.AddMemberWindow.open(function(o){			
					CloudDisk.Admin.SessionMgr.addUser({
					    user_name: o.email,
						user_type: o.memberType,
						email: o.email,
						password: '123456',
						group_id: o.groupId
					}, function(){			    

					});
				});
			});
			
			el.find('.search-bar input').keyup(function(){
			    var filteredMembers = [];
				var key = $(this).val();
				var body = el.find('.adm-member-body');
				for(var i = 0; i < members.length; ++i){
				    var m = members[i];
					if(m.user_name.indexOf(key) > -1 || m.email.indexOf(key) > -1){
					    filteredMembers.push(m);
					}
				}
				body.empty();
				for(var i = 0; i < filteredMembers.length; ++i){
				    body.append(that.createItem(filteredMembers[i]));
				}
			});
		},
		
		activate: function(){
		    var that = this;
		    el.show();
			if(!initialized){
			    that.init();
				initialized = true;
			}
			el.find('.search-bar input').val('');
			
			CloudDisk.Admin.SessionMgr.queryUserList(function(data){			    
				var body = el.find('.adm-member-body');
				members = data.result;
				
				body.empty();
				for(var i = 0; i < members.length; ++i){
				    body.append(that.createItem(members[i]));
				}
			});
		},
		
		createItem: function(data){
			var item = $('<div class="adm-member-item"></div>');
			var that = this;
			item.html(memberItemTpl.render(data));
			item.attr('member-id', data.user_id);
			
			item.on('click', function(e){
				var tar = e.target;
				var itemId = $(this).attr('member-id');
				if(tar.getAttribute('action') == 'group'){
					CloudDisk.Admin.UpdateGroupWindow.open(function(o){
					    for(var i = 0; i < members.length; ++i){
						    if(members[i].user_id + '' == itemId){
							    members[i].group_id = o.groupId;
								members[i].group_name = o.groupName;
								el.find('div[member-id=' + itemId + ']').html(memberItemTpl.render(members[i]));
								
								CloudDisk.Admin.SessionMgr.updateUser(members[i], function(){			    

								});
								break;
							}
						}
					});
				}
			});
			return item;
		}
	};
})();

CloudDisk.Admin.AddMemberWindow = (function(){
    var tpl = [
	    '<div class="error-message"></div>',
	    '<div class="" style="display: block; width: auto; min-height: 0px; height: 431px;padding:20px;">',		    
            '<div class="form-item">', 
			    '<label>Email:</label>',
				'<input name="email" type="text" class="input-text">',
			'</div>',
			'<div class="form-item">',
			    '<label>权限:</label>',
				'<select name="memberType" class="selectpicker">',
				    '<option value="1">管理员</option>',
					'<option value="2" selected>普通用户</option>',
				'</select>',
			'</div>',
			'<div class="form-item">',
			    '<label>用户组:</label>',
				'<select name="group" class="selectpicker">',
				    '<option value="">------</option>',
				'</select>',
			'</div>',			
            '<div class="add-member-ops" style="margin-top:20px;">',
			    '<button action="addMember" class="primary-btn">确定</button>',
				'<button action="cancel" class="secondary-btn">取消</button>',
			'</div>',
		'</div>'].join('');
	
	return new CloudDisk.Window({
	    title: '邀请成员',
		content: tpl,
		width: 400,
		height: 230,
		open: function(callback){
		    this.callback = callback;
	        CloudDisk.Util.showMask();
			this.el.show();
			this.position();

			var that = this;
			CloudDisk.Admin.SessionMgr.queryGroupList(function(data){			    
				var d = data.result;
				var select = that.el.find('select[name=group]');
				select.empty();
				select.append('<option value="">------</option>');
				for(var i = 0; i < d.length; ++i){
				    select.append('<option value="' + d[i].group_id + 
					    '">' + d[i].group_name + '</option>');
				}	
			});		

			that.el.find('.error-message').hide();
			that.el.find('input[name=email]').val('');
			that.el.find('select[name=memberType]').val(2);
			that.el.find('select[name=group]').val('');          			
			
			//this.el.find('.selectpicker').selectpicker();		
		},
		clickHandler: function(e, o){
		    var tar = e.target;
			if(tar.getAttribute('action') == 'addMember'){
			    var email = o.el.find('input[name=email]').val();
				var memberType = o.el.find('select[name=memberType]').val();
				var group = o.el.find('select[name=group]').val();
				
				if(!email){
				    o.el.find('.error-message').html('Email不能为空').show();
					return;
				} else if(!/^([a-zA-Z0-9_.+-])+\@(([a-zA-Z0-9-])+\.)+([a-zA-Z0-9]{2,4})+$/.test(email)){
				    o.el.find('.error-message').html('Email格式不正确').show();
					return;
				}
				
			    o.callback({
					email: email,
					memberType: memberType,
					groupId: group
				});
			    o.close();
			} else if(tar.getAttribute('action') == 'cancel'){
			    o.close();
			}
		}
	});
})();

CloudDisk.Admin.UpdateGroupWindow = (function(){
    var tpl = '';
		
	var itemTpl = $.templates([
	    '<div class="adm-group-item" group-id="{{:group_id}}">',
	        '<span class="group-name">{{:group_name}}</span>',
			'<span class="group-desc">{{:group_desc}}</span>',
	    '</div>'
	].join(''));
	
	return new CloudDisk.Window({
	    title: '全部分组',
		content: tpl,
		open: function(callback){
		    this.callback = callback;
	        CloudDisk.Util.showMask();
			this.el.show();
			this.position();	
			
			var body = this.el.find('.window-body');
			CloudDisk.Admin.SessionMgr.queryGroupList(function(data){			    
				var d = data.result;
				body.empty();
				for(var i = 0; i < d.length; ++i){
				    body.append($(itemTpl).render(d[i]));
				}	
			});			
		},
		
		clickHandler: function(e, o){
		    var tar = e.target;
			if(tar.getAttribute('group-id')){
			    o.callback({
				    groupId: tar.getAttribute('group-id'),
				    groupName: tar.innerHTML
				});
			    o.close();
			}
		}
	});
})();

